package client;

import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;

import javax.swing.JLabel;
import javax.swing.JPanel;

public class NavigationPanel extends JPanel{
	
	private NavigatorItem home;
	private NavigatorItem login;
	private NavigatorItem register;
	private User user;
	
	public NavigationPanel(){
		setLayout(null);
		Dimension d = new Dimension(20,20);
		setSize(d);
		setPreferredSize(d);
		home = new NavigatorItem("Home");
		home.addMouseListener(new MouseAdapter(){
			public void mousePressed(MouseEvent e) {
				Client.c.setHome();
			}
		});
		home.setLocation(2,0);
		add(home);
		logout();
	}
	
	public void login(User user) {
		this.user = user;
		if(register!=null||login!=null){
			remove(register);
			remove(login);
		}
		register = new NavigatorItem("Account");
		register.addMouseListener(new MouseAdapter(){
			public void mousePressed(MouseEvent e) {
				createAccount();
			}
		});
		login = new NavigatorItem("Logout");
		login.addMouseListener(new MouseAdapter(){
			public void mousePressed(MouseEvent e) {
				Client.c.logout();
				logout();
			}
		});
		add(register);
		add(login);
		updateUI();
	}
	
	public void logout(){
		this.user = null;
		if(register!=null||login!=null){
			remove(register);
			remove(login);
		}
		register = new NavigatorItem("Register");
		register.addMouseListener(new MouseAdapter(){
			public void mousePressed(MouseEvent e) {
				Client.c.register();
			}
		});
		login = new NavigatorItem("Login");
		login.addMouseListener(new MouseAdapter(){
			public void mousePressed(MouseEvent e) {
				Client.c.login();
			}
		});
		add(register);
		add(login);
		updateUI();
	}
	
	
	public void createAccount(){
		Client.c.setCenter(new AccountPanel(user));
	}
	
	protected void paintComponent(Graphics g){
		register.setLocation(this.getWidth()-register.getWidth(),0);
		login.setLocation(this.getWidth()-register.getWidth()-login.getWidth(),0);
		super.paintComponent(g);
	}
	
	private class NavigatorItem extends JLabel{
		
		public NavigatorItem(String s){
			super(s);
			Dimension d = new Dimension(100,20);
			setSize(d);
			setPreferredSize(d);
		}
		
	}

}
